package com.wwdx.controller.rest;

import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.google.common.collect.Maps;
import com.wwdx.controller.base.BaseController;
import com.wwdx.model.dto.ResultBean;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * @author liguoliang
 */
@RestController
@RequestMapping("wechat")
@ApiIgnore
public class RestWechatAuthController extends BaseController{

    /**
     * 蜜读服务号登录
     * @param code 授权码
     */
    @RequestMapping(value = "/login")
    public ResultBean wechatLogin(String code){
        return this.execute(code,miDuWxService);
    }

    private ResultBean execute(String code, WxMpService service){
        try {
            WxMpUser wxMpUser = service.oauth2getUserInfo(service.oauth2getAccessToken(code), null);
            Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);
            map.put("userInfo",wwdxPersonalService.executePersonalInfo(wxMpUser,service));
            return renderSuccess(map);
        }catch (WxErrorException e){
            return renderError(e.getMessage());
        }
    }

    /**
     * 测试服务号登录
     * @param code 授权码
     */
    @RequestMapping(value = "/login2")
    public ResultBean wechatLogin2(String code){
        return execute(code,testWxService);
    }

    /**
     * 获取微信小程序登录授权信息
     * @param code 授权码
     */
    @RequestMapping("/wxma/login")
    public ResultBean login(String code) {
        if (StringUtils.isBlank(code)) {
            return renderError("empty jscode");
        }
        try {
            return renderSuccess(this.miDuWxMaService.getUserService().getSessionInfo(code));
        } catch (WxErrorException e) {
            this.logger.error(e.getMessage(), e);
            return renderError(e.toString());
        }
    }

    /**
     * 获取用户信息接口
     * @param sessionKey    会话密钥
     * @param signature     数据签名
     * @param rawData       微信用户基本信息
     * @param encryptedData 消息密文
     * @param iv            加密算法的初始向量
     */
    @RequestMapping("/wxma/info")
    public ResultBean info(String sessionKey, String signature, String rawData, String encryptedData, String iv) {
        WxMaUserService userService = this.miDuWxMaService.getUserService();
        boolean valid = userService.checkUserInfo(sessionKey, rawData, signature);
        if (valid) {
            //解密用户信息
            WxMaUserInfo userInfo = userService.getUserInfo(sessionKey, encryptedData, iv);
            return renderSuccess(wwdxPersonalService.executePersonalInfo(userInfo,miDuWxMaService));
        }
        return renderError("user check failed");
    }

    /**
     * 获取用户绑定手机号信息
     * @param sessionKey    会话密钥
     * @param signature     数据签名
     * @param rawData       微信用户基本信息
     * @param encryptedData 消息密文
     * @param iv            加密算法的初始向量
     */
    @RequestMapping("/wxma/phone")
    public ResultBean phone(String sessionKey, String signature, String rawData, String encryptedData, String iv) {
        WxMaUserService userService = this.miDuWxMaService.getUserService();
        boolean valid = userService.checkUserInfo(sessionKey, rawData, signature);
        if (valid) {
            //解密手机号信息
            WxMaPhoneNumberInfo phoneNoInfo = userService.getPhoneNoInfo(sessionKey, encryptedData, iv);
            return renderSuccess(phoneNoInfo);
        }
        return renderError("user check failed");
    }
}